import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import viteCompression from 'vite-plugin-compression'
import vueDevtools from 'vite-plugin-vue-devtools'

import Components from 'unplugin-vue-components/vite'

// npm install -D unplugin-icons unplugin-vue-components @iconify/json
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'

// npm install -D @vant/auto-import-resolver unplugin-vue-components unplugin-auto-import
import AutoImport from 'unplugin-auto-import/vite';
import { VantResolver } from '@vant/auto-import-resolver';

// npm install tailwindcss @tailwindcss/vite
import tailwindcss from '@tailwindcss/vite'

// https://vite.dev/config/
export default ({ mode }) => {
  const env = loadEnv(mode, process.cwd());
  return defineConfig({
    plugins: [
      vue(),
      tailwindcss(),
      vueDevtools(),
      // 自动按需引入vue3 api, vant组件
      AutoImport({
        resolvers: [VantResolver()],
      }),
      // 自动按需引入iconify组件, 自动引入vant组件
      Components({
        resolvers: [
          IconsResolver({
            prefix: 'icon'
          }),
          VantResolver()
        ]
      }),
      // 自动按需引入iconify图标
      Icons({
        autoInstall: true
      }),
      // gzip压缩 生产环境生成 .gz 文件
      env.VITE_BUILD_COMPRESS === 'gzip' && viteCompression({
        verbose: true,
        disable: false,
        threshold: 10240,
        algorithm: 'gzip',
        ext: '.gz',
      }),
    ].filter(Boolean),
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
      }
    },
    server: {
      port: 81,
      host: '0.0.0.0',
      open: true,
      proxy: {
        '/dev-api': {
          target: 'http://localhost:9090/NL_EBM_Web',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/dev-api/, '')
        },
      },
    }
  })
};